﻿'*******************************************************************************************
'* Copyright Brügmann Software GmbH, Papenburg
'* Author: Björn Kremer
'* Contact: codeplex<at>patorg.de
'* 
'* VB.CMIS is a VB.NET implementation of the Content Management Interoperability Services (CMIS) standard
'*
'* This file is part of VB.CMIS.
'* 
'* VB.CMIS is free software: you can redistribute it and/or modify
'* it under the terms of the GNU Lesser General Public License as published by
'* the Free Software Foundation, either version 3 of the License, or
'* (at your option) any later version.
'* 
'* VB.CMIS is distributed in the hope that it will be useful,
'* but WITHOUT ANY WARRANTY; without even the implied warranty of
'* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'* GNU Lesser General Public License for more details.
'* 
'* You should have received a copy of the GNU Lesser General Public License
'* along with VB.CMIS. If not, see <http://www.gnu.org/licenses/>.
'*******************************************************************************************
Imports System.ServiceModel.Syndication
Namespace Atom
   <Xml.Serialization.XmlRoot("entry", Namespace:=CmisNamespaceConstants.Atom)>
   Public Class CMISAtomEntryCmisObject
      Inherits CMISAtomEntry(Of CMISObjects.CmisObject)


      Public Property CMISRAContent As CMISObjects.ContentContainer
      Public Property AtomContent As CMISObjects.AtomContent

      Public Property BulkUpdate As CMISObjects.CmisBulkUpdateType
   


      Public Sub New()
         MyBase.New()
      End Sub
      Public Sub New([object] As CMISObjects.CmisObject, atomId As String, atomTitle As String, atomCreationDate As DateTimeOffset, atomLastUpdatedTime As DateTimeOffset, atomAuthors As List(Of SyndicationPerson), Optional pathSegment As String = Nothing, Optional relativePathSegment As String = Nothing)
         MyBase.New([object], atomId, atomTitle, atomCreationDate, atomLastUpdatedTime, atomAuthors)
         _PathSegment = pathSegment
         _RelativePathSegment = relativePathSegment
      End Sub

      Private _PathSegment As String
      Public ReadOnly Property PathSegment As String
         Get
            Return _PathSegment
         End Get
      End Property
      Private _RelativePathSegment As String
      Public ReadOnly Property RelativePathSegment As String
         Get
            Return _RelativePathSegment
         End Get
      End Property
      Public Overrides Sub ReadXml(reader As Xml.XmlReader)
         '  MyBase.ReadXml(reader)
         Dim doc As New System.Xml.XmlDocument()
         doc.Load(reader)

         Dim entryNode As System.Xml.XmlNode = Common.GetItemCaseInsensitiveFromXML(doc, "entry")

         Dim atomPrefix As List(Of String) = Common.GetPrefixOfNamespace(entryNode, CMISObjectModel.Constants.CmisNamespaceConstants.Atom)
         Dim cmisraPrefix As List(Of String) = Common.GetPrefixOfNamespace(entryNode, CMISObjectModel.Constants.CmisNamespaceConstants.Cmisra)
       



         Dim objectNode As Xml.XmlNode = Nothing
         Dim cmisRAContentNode As Xml.XmlNode = Nothing
         Dim atomContentNode As Xml.XmlNode = Nothing
         Dim bulkUpdate As Xml.XmlNode = Nothing
         For Each child As System.Xml.XmlElement In entryNode.ChildNodes
            Dim lName As String = Common.GetNodeNameWithoutPrefix(child).ToLowerInvariant
            If lName.Equals("object") Then
               objectNode = child
            ElseIf lName.Equals("content") Then
               Dim prefix As String = child.Prefix
               If prefix Is Nothing Then
                  prefix = String.Empty
               Else
                  prefix = prefix
               End If
               If cmisraPrefix.Contains(prefix) Then
                  cmisRAContentNode = child
               ElseIf atomPrefix.Contains(prefix) Then
                  atomContentNode = child
               End If
            ElseIf lName.Equals("bulkupdate") Then
               bulkUpdate = child
            End If
         Next
       
         If cmisRAContentNode IsNot Nothing Then
            Using stream As New IO.StringReader(cmisRAContentNode.OuterXml)
               Dim serializer As New Xml.Serialization.XmlSerializer(GetType(CMISObjects.ContentContainer))
               Me.CMISRAContent = DirectCast(serializer.Deserialize(stream), CMISObjects.ContentContainer)
            End Using
         End If
         If atomContentNode IsNot Nothing Then
            Using stream As New IO.StringReader(atomContentNode.OuterXml)
               Dim serializer As New Xml.Serialization.XmlSerializer(GetType(CMISObjects.AtomContent))
               Me.AtomContent = DirectCast(serializer.Deserialize(stream), CMISObjects.AtomContent)
            End Using
         End If
         If bulkUpdate IsNot Nothing Then
            Using stream As New IO.StringReader(bulkUpdate.OuterXml)
               Dim serializer As New Xml.Serialization.XmlSerializer(GetType(CMISObjects.CmisBulkUpdateType))
               Me.BulkUpdate = DirectCast(serializer.Deserialize(stream), CMISObjects.CmisBulkUpdateType)
            End Using
         End If
         If objectNode IsNot Nothing Then
            Dim typeId As String = Nothing
            For Each child As System.Xml.XmlElement In objectNode.ChildNodes
               Dim lName As String = Common.GetNodeNameWithoutPrefix(child).ToLowerInvariant
               If lName.Equals("properties") Then
                  typeId = ParsePropertiesNode(child)
                  Exit For
               End If
            Next
            If Not String.IsNullOrEmpty(typeId) Then
               typeId = typeId.ToLowerInvariant
               If typeId.Equals("cmis:document") Then
                  Me.Object = Parse(Of CMISObjects.CmisDocumentObject)(objectNode)
               ElseIf typeId.Equals("cmis:folder") Then
                  Me.Object = Parse(Of CMISObjects.CmisFolderObject)(objectNode)
               ElseIf typeId.Equals("cmis:relationship") Then
                  Me.Object = Parse(Of CMISObjects.CmisRelationshipObject)(objectNode)
               ElseIf typeId.Equals("cmis:policy") Then
                  Me.Object = Parse(Of CMISObjects.CmisPolicyObject)(objectNode)
               Else
                  Me.Object = Parse(Of CMISObjects.CMISUnknownObject)(objectNode)
               End If
            Else
               Me.Object = Parse(Of CMISObjects.CMISUnknownObject)(objectNode)
            End If

         End If
      End Sub


      Private Function ParsePropertiesNode(objectNode As Xml.XmlNode) As String
         For Each child As System.Xml.XmlElement In objectNode.ChildNodes
            If child.Attributes("propertyDefinitionId") IsNot Nothing AndAlso child.Attributes("propertyDefinitionId").InnerText.ToLowerInvariant.Equals("cmis:basetypeid") Then
               Return child.InnerText
            End If
         Next
         Return Nothing
      End Function
      Private Shared Function Parse(Of T)(node As Xml.XmlNode) As T
         Using stream As New IO.StringReader(node.OuterXml)
            Dim serializer As New Xml.Serialization.XmlSerializer(GetType(T))
            Return DirectCast(serializer.Deserialize(stream), T)
         End Using
      End Function
   End Class
End Namespace